<?php

namespace Ignus\View\Node\Expression;

use Ignus\View\Node\Expression;
use Ignus\View\Compiler;
use Ignus\View\NodeInterface;

class BlockReference extends Expression
{
    public function __construct(NodeInterface $name, $asString = false, $lineno, $tag = null)
    {
        $attributes = array('as_string' => $asString, 'output' => false);
        parent::__construct(array('name' => $name), $attributes, $lineno, $tag);
    }

    public function compile(Compiler $compiler)
    {
        if ($this->getAttribute('as_string')) {
            $compiler->raw('(string) ');
        }

        if ($this->getAttribute('output')) {
            $compiler
                ->addDebugInfo($this)
                ->write("\$this->displayBlock(")
                ->subcompile($this->getNode('name'))
                ->raw(", \$context, \$blocks);\n");
        }
        else {
            $compiler
                ->raw("\$this->renderBlock(")
                ->subcompile($this->getNode('name'))
                ->raw(", \$context, \$blocks)");
        }
    }
}
